﻿using System;
using System.Xml;
using System.Web;

namespace ZhuGe.Tools.XML
{
    /// <summary>
    /// 版权： Copyright by SEM IT Department
    /// 版本： 0.0.1
    /// 文件： XmlBook.Com.Sem.Tools.Log.cs
    /// 目的： 提供一个写LOG的方法（就是将错误写入XML记录当中）
    /// 作者： 欧远宁 @2005-04-0Array
    /// 邮箱： outrace@soueast-motor.com
    /// 修改：
    /// </summary>
    public class Log
    {
        #region 私有成员
        private HttpContext objContext = HttpContext.Current;
        /// <summary>
        /// 日志文件路径
        /// </summary>
        private string logFile = null;
        /// <summary>
        /// 操作人员
        /// </summary>
        private string strUser = null;
        /// <summary>
        /// 所属版块
        /// </summary>
        private string strDepartment = null;
        /// <summary>
        /// 正在操作的文件名
        /// </summary>
        private string strFileName = null;
        /// <summary>
        /// 操作时间
        /// </summary>
        private string strTime = null;
        /// <summary>
        /// 错误描述
        /// </summary>
        private string strDescription = null;
        #endregion

        #region 公共属性
        /// <summary>
        /// 操作人员
        /// </summary>
        public string StrUser
        {
            get { return this.strUser; }
            set { this.strUser = value; }
        }
        /// <summary>
        /// 正在操作的文件名
        /// </summary>
        public string StrFileName
        {
            get { return this.strFileName; }
            set { this.strFileName = value; }
        }
        /// <summary>
        /// 所属版块
        /// </summary>
        public string StrDepartment
        {
            get { return this.strDepartment; }
            set { this.strDepartment = value; }
        }
        /// <summary>
        /// 操作时间
        /// </summary>
        public string StrTime
        {
            get { return this.strTime; }
            set { this.strTime = value; }
        }
        /// <summary>
        /// 错误描述
        /// </summary>
        public string StrDescription
        {
            get { return this.strDescription; }
            set { this.strDescription = value; }
        }
        #endregion


        public Log()
        {
            //
            // TODO: 提供一个写LOG的方法（就是将错误写入XML记录当中）
            //
        }

        /// <summary>
        /// 将内容写入日志文件
        /// </summary>
        public void WriteLog()
        {
            this.logFile = this.objContext.Server.MapPath("./Log/log.config");
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(this.logFile);
                XmlElement newLog = doc.CreateElement("Log");


                XmlElement newUser = doc.CreateElement("User");
                newUser.InnerText = this.strUser;
                newLog.AppendChild(newUser);

                XmlElement newDepartment = doc.CreateElement("Department");
                newDepartment.InnerText = this.strDepartment;
                newLog.AppendChild(newDepartment);

                XmlElement newFileName = doc.CreateElement("FileName");
                newFileName.InnerText = this.StrFileName;
                newLog.AppendChild(newFileName);

                XmlElement newTime = doc.CreateElement("Time");
                newTime.InnerText = DateTime.Now.ToString();
                newLog.AppendChild(newTime);

                XmlElement newDescription = doc.CreateElement("Description");
                newDescription.InnerText = this.strDescription;
                newLog.AppendChild(newDescription);

                doc.DocumentElement.AppendChild(newLog);
                doc.Save(this.logFile);
            }
            catch (Exception ex)
            {
                HttpContext objContext = HttpContext.Current;
                objContext.Response.Write(ex.Message);
            }
            finally
            {
            }
        }
    }
}
